package com.example.androidhive;

import java.io.BufferedReader;import com.google.android.gms.location.LocationListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends Activity implements
//OnMapClickListener, OnMapLongClickListener 
OnMarkerClickListener {
		
	Button mBtnFind,zz;
	GoogleMap mMap;
	EditText etPlace;
	private final LatLng LOCATION_BURNABY = new LatLng(50.97235, 18.21807);
	private GoogleMap map;
	 private LocationManager locationManager;
	 private static final long MIN_TIME = 400;
	 private static final float MIN_DISTANCE = 1000;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mBtnFind = (Button) findViewById(R.id.btn_show);
		zz = (Button) findViewById(R.id.btngps);
		mMap = ((MapFragment) getFragmentManager()
					.findFragmentById(R.id.map)).getMap();
		etPlace = (EditText) findViewById(R.id.et_place);
		mBtnFind.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {	
				String location = etPlace.getText().toString();
				if(location==null || location.equals("")){
					Toast.makeText(getBaseContext(), "No Place is entered", Toast.LENGTH_SHORT).show();
					return;
				}
				String url = "https://maps.googleapis.com/maps/api/geocode/json?";					
				try {
					location = URLEncoder.encode(location, "utf-8");
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				String address = "address=" + location;
				String sensor = "sensor=false";
				url = url + address + "&" + sensor;
				DownloadTask downloadTask = new DownloadTask();
				downloadTask.execute(url);
			}
		});
		zz.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {	
/*
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(bestProvider);
LocationListener loc_listener = new LocationListener() {

    public void onLocationChanged(Location l) {}

    public void onProviderEnabled(String p) {}

    public void onProviderDisabled(String p) {}

    public void onStatusChanged(String p, int status, Bundle extras) {}
};
//locationManager.requestLocationUpdates(bestProvider, 0, 0, loc_listener);
location = locationManager.getLastKnownLocation(bestProvider);double lat,lon,latitude = 0;
double longitude = 0;
try {
    lat = location.getLatitude();
    lon = location.getLongitude();
} catch (NullPointerException e) {
    lat = -1.0;
    lon = -1.0;
}*/
//Toast.makeText(this, "Myd"+lat+"-"+lon, Toast.LENGTH_LONG).show();
			}
		});
		mMap.setMyLocationEnabled(true); 	 mMap.setOnMarkerClickListener(this);
		
	}
	
	private String downloadUrl(String strUrl) throws IOException{
        String data = "";
        InputStream iStream = null;
        HttpURLConnection urlConnection = null;
        try{
                URL url = new URL(strUrl);
                urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.connect();
                iStream = urlConnection.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
                StringBuffer sb  = new StringBuffer();
                String line = "";
                while( ( line = br.readLine())  != null){
                        sb.append(line);
                }
                data = sb.toString();
                br.close();

        }catch(Exception e){
                Log.d("Exception while downloading url", e.toString());
        }finally{
                iStream.close();
                urlConnection.disconnect();
        }

        return data;
        
	}

	
	 /** A class, to download Places from Geocoding webservice */
    private class DownloadTask extends AsyncTask<String, Integer, String>{
            String data = null;
            @Override
            protected String doInBackground(String... url) {
                    try{                    		
                            data = downloadUrl(url[0]);
                    }catch(Exception e){
                             Log.d("Background Task",e.toString());
                    }
                    return data;
            }
            @Override
            protected void onPostExecute(String result){
            		ParserTask parserTask = new ParserTask();
                    parserTask.execute(result);
            }
    }
	class ParserTask extends AsyncTask<String, Integer, List<HashMap<String,String>>>{
		JSONObject jObject;
		@Override
		protected List<HashMap<String,String>> doInBackground(String... jsonData) {
			List<HashMap<String, String>> places = null;			
			GeocodeJSONParser parser = new GeocodeJSONParser();
	        try{
	        	jObject = new JSONObject(jsonData[0]);
	            places = parser.parse(jObject);
	        }catch(Exception e){
	                Log.d("Exception",e.toString());
	        }
	        return places;
		}
		@Override
		protected void onPostExecute(List<HashMap<String,String>> list){			
		//	mMap.clear();
			for(int i=0;i<list.size();i++){
	            MarkerOptions markerOptions = new MarkerOptions();
	            HashMap<String, String> hmPlace = list.get(i);
	            double lat = Double.parseDouble(hmPlace.get("lat"));	            
	            double lng = Double.parseDouble(hmPlace.get("lng"));
	            String name = hmPlace.get("formatted_address");
	            LatLng latLng = new LatLng(lat, lng);
	            EditText f=(EditText) findViewById(R.id.et_place);
	            f.setText(String.valueOf(lat+" "+lng));
	            markerOptions.position(latLng);
	            markerOptions.title(name);
	            mMap.addMarker(markerOptions);    
                if(i==0)
                	mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
            }            
		}
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onMarkerClick(Marker marker) {
		// TODO Auto-generated method stub
		return false;
	}
}
